// created on 8/10/2005 at 10:07 AM
// project created on 8/10/2005 at 7:59 AM

using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using SysReport;

namespace SysReport
{
	class OutputClass
	{
		public static void Run(string system)
		{
			string distqver = null;
			if (system == "conary")
			{
				distqver = Conary.GroupDist();
			}
						
			// Get kernel version
			string kernel = System.Environment.OSVersion.Version.ToString();
			// Get Total Memory
			ArrayList memInfo = new ArrayList();
			memInfo.AddRange(Collection.TotalMemory());
			object totalMem = Convert.ToInt32(memInfo[0])/1024;
			object freeMem = Convert.ToInt32(memInfo[1])/1024;
			object swapTotal = Convert.ToInt32(memInfo[2])/1024;
			object swapFree = Convert.ToInt32(memInfo[3])/1024;

			// Get CPU Info
			ArrayList cpuInfo = new ArrayList();
			cpuInfo.AddRange(Collection.CPU());
			object cpu = cpuInfo[0];
			object cpuFreq = cpuInfo[1];
			
			// Get uptime
			decimal uptime = Collection.Uptime();
			uptime = uptime/60/60;
			string unit = "Hours";
			if ( uptime > 24 )
			{
			     uptime = uptime/24;
			     unit = "Days";
			}
			
			// Get OS info
			string os = Collection.OS(system);

			// Get hostname info
			string hostname = Collection.getHost();
			
			// Get package list
			string [] packages;
			if (system == "redhat" || system == "fedora")
			{
				packages = RPM.Packages();
			}
			else
			{
				packages = Conary.Packages();
			}

			DateTime fullDate = DateTime.Today;
			string date = fullDate.Month + "/" + fullDate.Day + "/" + fullDate.Year;
			
			
			// Create XML Output			
			Console.WriteLine("<?xml version=\"1.0\"?>");
			Console.WriteLine("<sysreport host=\""+ hostname + "\" date=\""+ date +"\">");
			Console.WriteLine("\t<info>");
			Console.WriteLine("\t\t<os>"+os+"</os>");
			if (system == "conary")
			{
				Console.WriteLine("\t\t<distVersion>"+distqver+"</distVersion>");
			}
			Console.WriteLine("\t\t<kernel>"+kernel+"</kernel>");
			Console.WriteLine("\t\t<cpu>"+cpu+"</cpu>");
			Console.WriteLine("\t\t<cpuFreq unit=\"MHz\">"+cpuFreq+"</cpuFreq>");
			Console.WriteLine("\t\t<uptime>"+System.Decimal.Round(uptime, 2) + " " + unit+"</uptime>");
			Console.WriteLine("\t\t<totalMemory unit=\"MB\">"+totalMem+"</totalMemory>");
			Console.WriteLine("\t\t<freeMemory unit=\"MB\">"+freeMem+"</freeMemory>");
			Console.WriteLine("\t\t<totalSwap unit=\"MB\">"+swapTotal+"</totalSwap>");
			Console.WriteLine("\t\t<freeSwap unit=\"MB\">"+swapFree+"</freeSwap>");
			Console.WriteLine("\t</info>");
			Console.WriteLine("\t<packages>");
			
			if (system == "conary")
			{  
				for(int i=0;i < packages.Length;i++)
				{
					string [] package = System.Text.RegularExpressions.Regex.Split(packages[i], "\\s+");
					if (package[0].Length>0)
					//if (package[0].Length>0 && package[1].Length>0)
					{
						string packageName = package[0];
						//string [] fullVersion = System.Text.RegularExpressions.Regex.Split(package[1], "/");
						string [] fullVersion = System.Text.RegularExpressions.Regex.Split(package[0], "/");
						string version = fullVersion[fullVersion.Length-1];
						string label = fullVersion[fullVersion.Length-2];
						Console.WriteLine("\t\t\t<package label=\""+label+"\" version=\""+version+"\">" + packageName + "</package>");
					}
				}
			} 
			else if (system == "redhat" || system == "fedora")
			{ 
				for(int i=0;i < packages.Length;i++)
				{	
					string foo = @"(?<name>.+?)-(?<version>[0-9].*)";
                        		Regex regex = new Regex (@foo);
					if (packages[i].Length>0)
					{
                    				Match match = regex.Match(packages[i]);
						string packageName = match.Groups["name"].ToString();
						string version = match.Groups["version"].ToString();
						Console.WriteLine("\t\t\t<package version=\""+version+"\">" + packageName + "</package>");
					}
				}
			}						
			Console.WriteLine("\t\t</packages>");
			Console.WriteLine("</sysreport>"); 
		}
	}
}
